﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.FileShare.Interface;
using Hi_Baidu.Entry.CollectionObject;
using System.Windows.Forms;
using Hi_Baidu.FileShare.User;

namespace Hi_Baidu.FileShare
{
    public delegate void CreateFileShareUser(object Sender, FileShareEventArgs e);
    [Serializable]
    public class FileShareProcessor : MarshalByRefObject, IFileShareProcessor
    {
        private String key;
        private List<FileShareUser> FileShareUserCollection = new List<FileShareUser>();

        /// <summary>
        ///     查询指定用户是否存在于用户集合中
        /// </summary>
        /// <param name="user">指定用户</param>
        /// <returns>返回false,  则表示不存在</returns>
        public bool Equals(IMetaData user)
        {
            foreach (IMetaData item in FileShareUserCollection)
            {
                if(item.Key == user.Key)
                {
                    return true;
                }
            }
            return false;
        }

        public FileShareProcessor()
        {
            key = "ID-KEY:FILE-SHARE-PROGRAM-LAYER-TIME-" + DateTime.Now.ToString();
        }

        private FileShareUserManager fileShareUserManager = new FileShareUserManager();
        public static event CreateFileShareUser CreateUserEventArgs;

        #region IFileShareProcessor 成员

        public bool CreateFileShareByUser(IMetaData user)
        {
            if(!Equals(user))
            {
                FileShareUserCollection.Add(user as FileShareUser);
                if (CreateUserEventArgs != null)
                {
                    FileShareEventArgs e = new FileShareEventArgs(user);
                    CreateUserEventArgs(this, e);
                }
            }
            return true;
        }

        public bool RemoveFielShareByUser(IMetaData user)
        {
            throw new NotImplementedException();
        }

        public bool UpdateFileShareByUser(IMetaData user)
        {
            throw new NotImplementedException();
        }

        public List<IMetaData> GetShareUsers()
        {
            return fileShareUserManager.GetMember();
        }

        public IMetaData GetShareUserByKey(string Key)
        {
            if(this.fileShareUserManager.Equals(Key))
            {
                return fileShareUserManager.GetMemberByKey(Key);
            }
            return null;
        }

        #endregion


    }

    [Serializable]
    public class FileShareEventArgs : EventArgs
    {
        private IMetaData _User;

        public IMetaData User
        {
          get { return _User; }
          set { _User = value; }
        }

        public FileShareEventArgs(IMetaData User)
        {
            _User = User;
        }
    }
}
